/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralanalysis;

import org.openaecfm.model.kernel.Object;
import org.openaecfm.model.structuralanalysis.ActionSourceTypeEnum;
import org.openaecfm.model.structuralanalysis.ActionTypeEnum;
import org.openaecfm.model.structuralanalysis.LoadGroupTypeEnum;
import org.openaecfm.model.structuralanalysis.StructuralLoadGroup;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class StructuralLoadGroupIO extends
		AbstractEntityInstanceInputImpl<StructuralLoadGroup> implements
		IEntityInstanceOutput<StructuralLoadGroup>,
		IEntityInstanceUtilOutput<StructuralLoadGroup>,
		IEntityInstanceUtilInput<StructuralLoadGroup> {
	private IEntityInstanceUtilInput<org.openaecfm.model.kernel.Object> utilInput;
	private IEntityInstanceUtilOutput<org.openaecfm.model.kernel.Object> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Object> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(StructuralLoadGroup pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSTRUCTURALLOADGROUP", appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected StructuralLoadGroup doCreateObject() {
		return new StructuralLoadGroup();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			StructuralLoadGroup pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<org.openaecfm.model.kernel.Object> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			StructuralLoadGroup pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		EnumerationParameter pretype = (EnumerationParameter) sei
				.getParameter(5);
		pojo.setPredefinedType(LoadGroupTypeEnum.valueOf(pretype.toEnumString()));

		// Set ActionType
		EnumerationParameter actiontype = (EnumerationParameter) sei
				.getParameter(6);
		pojo.setActionType(ActionTypeEnum.valueOf(actiontype.toEnumString()));

		// Set ActionSource
		EnumerationParameter actionsource = (EnumerationParameter) sei
				.getParameter(7);
		pojo.setActionSource(ActionSourceTypeEnum.valueOf(actionsource
				.toEnumString()));

		// Set Coefficient
		Parameter coefficient = sei.getParameter(8);
		if (coefficient.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) coefficient;
			pojo.setCoefficient(r.toDouble());
		}

		// Set Purpose
		Parameter purpose = sei.getParameter(9);
		if (purpose.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) purpose;
			pojo.setPurpose(s.toUTF8());
		}
	}

	@Override
	public void exportSPF(StructuralLoadGroup pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		sei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));

		// Set ActionType
		sei.addParameter(new EnumerationParameter(pojo.getActionType()));

		// Set ActionSource
		sei.addParameter(new EnumerationParameter(pojo.getActionSource()));

		// Set Coefficient
		if (pojo.getCoefficient() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getCoefficient()));
		}

		// Set Purpose
		if (pojo.getPurpose() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getPurpose(),
					StringParameter.UTF_8));
		}
	}
}
